<template>
  <div>
    <el-dialog
      title="修改信息"
      :visible.sync="dialogVisible"
      width="400px"
      :center="true"
      :before-close="handleClose"
      @close="onClose"
    >
      <el-form ref="form" :model="form">
        <el-form-item label="商户名称">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item label="商户地址">
          <el-input v-model.trim="form.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSave">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  updateCorp // 修改商户
} from "@/api/corpConfig";
export default {
  props: {
    options: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        address: ""
      }
    };
  },
  created() {
    this.form = Object.assign({}, this.options);
    this.dialogVisible = true;
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 保存
    onSave() {
      let arr = [],
        obj = {
          id: this.form.id,
          name: this.form.name,
          address: this.form.address
        };
      arr.push(obj);
      updateCorp(arr).then(
        res => {
          this.dialogVisible = false;
          this.$message({
            type: "success",
            message: "操作成功"
          });
        },
        err => {
          this.$message({
            type: "warning",
            message: "操作失败,请重新操作"
          });
        }
      );
    },
    // 弹窗关闭前执行
    onClose() {
      this.$emit("updateCorpDialog", this.form);
    }
  }
};
</script>

